// MatrixCalibratorView.h : interface of the CMatrixCalibratorView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MATRIXCALIBRATORVIEW_H__F931E760_AFC6_41A8_A5C3_B8A1E74B8485__INCLUDED_)
#define AFX_MATRIXCALIBRATORVIEW_H__F931E760_AFC6_41A8_A5C3_B8A1E74B8485__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define	GLASS	0
#define PCB		1

#include "../Include/MIOCntr.h"
#include "../Include/DioCntr.h"
#include "../Library/Matrox/GrabBuffer.h"
#include "../Library/ComDevice/Light.h"
#include "../Library/ComDevice/comm.h"
#include "LightTable.h"

#ifdef _MV_7_
#include "LightCtrl.h"
#endif

#include "Config.h"
#include "MainFrm.h"
#include "MatrixCalibrator.h"

class CMilDisplay;
class CMatrixCalibratorDoc;

#define CENTER_AREA		100

struct EmCode
{
	enum
	{
		None,
			DioFault,
			EStopSw,
			AreaSensor,
			FrontDoorOpened,
			RearDoorOpened,
			AirSupply,
			XAxisServoAlarm,
			XAxisPosLimit,
			XAxisNegLimit,
			YAxisServoAlarm,
			YAxisPosLimit,
			YAxisNegLimit,
			ConveyorTimeOut,
			PCBJammed,
			ManualConveyor,
			RailCoverOpened
	};
	static CString toString(long emCode);
	static BOOL IsPause(long emCode)
	{
		return emCode == XAxisPosLimit || emCode == XAxisNegLimit ||
			emCode == YAxisPosLimit || emCode == YAxisNegLimit ||
			emCode == ConveyorTimeOut || emCode == PCBJammed;
	}
	static BOOL IsCritical(long emCode)
	{
		return emCode == DioFault;
	}
};

class CMatrixCalibratorView : public CView ,CImageModifier
{
protected: // create from serialization only
	CMatrixCalibratorView();
	DECLARE_DYNCREATE(CMatrixCalibratorView)

/// Attributes
public:
	
private:

protected:
	static CMioCntr* m_pMioCntr;
	static CDioCntr* m_pDioCntr;
	static CGrabBuffer* m_pGrabBuffer;
	static CLightTable m_lightTable;
	static LightInfo m_lightInfo;
#ifdef _MV_7_
	//static CLightCtrl* m_pLightCtrl;
	static CLight* m_pLightCtrl;
#endif
#ifdef _MV_2_
	static CLight* m_pLightCtrl;
#endif
    
	CBufferExtender* m_pBufferExtender;
	CMilImage m_captureImage;
	CMilDisplay*	m_pMilDisplay;
	CSize m_framePelSize;
	CdblSize m_pelSize;

private:
	long	m_emCode;
	CComm	m_micomComm;

	long	m_horzRightIndex;
	long	m_horzLeftIndex;
	long	m_horzTopIndex;
	long	m_horzBottomIndex;
	int		m_Material;

/// Operations
public:
	CMatrixCalibratorDoc* GetDocument();
	static BOOL IsOriginRequired();
	static BOOL IsEstop();
	static CMioCntr* GetMioCntr();
	static CDioCntr* GetDioCntr();
	static CGrabBuffer* GetGrabBuffer(){ return m_pGrabBuffer;};
	void Invalidate();
	void ProcessImage(CMilImage* pMilImage);
	virtual void ModifyImage(CMilImage* pMilImage);
	void Paint(CDC* pDC);
	virtual void UpdateOverlay();
	virtual void UpdateFrameImageDlg(CMilImage* pMilImage);
	void SetFrameSize(CSize frameSize);
	BOOL InitMilLibrary();
	CSize GetFrameSize();
	static BOOL LightOn(LightInfo* pLightInfo, BOOL fWait);
	static BOOL LightOn();
	void MoveToOrigin();
	long MoveDelta(double dblPosX, double dblPosY, CalibrationPoint &cp, BOOL fWaitComplet = FALSE);
	CdblPoint GetRobotPos();
	long Move(double dblPosX, double dblPosY, BOOL fWaitComplete = FALSE, CStdioFile *file = NULL);
private:
	CdblPoint PixelToWorld(const CdblPoint& pelPos);
	BOOL IsEStop();
	void CreateMicom();
	void DestroyMicom();
protected:
	long OffsetMove(const CSize& dblOffset, long viewGrabType = 0, int camerno=CameraIndex::NoChange);
	void PrepareDC(HDC hDC);
	void StartupServo();
	BOOL WaitMoveDone();
	//void UpdateMilWindow(long width, long height);
	void StopMachine();
	void _MoveToOrigin();
	void InitDMIO();

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMatrixCalibratorView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
	protected:
	virtual void OnInitialUpdate(); // called first time after construct
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	//}}AFX_VIRTUAL

// Implementation
public:
	void SetMaterial(int material);
	void MoveToBase(CdblPoint dblBase);
	BOOL GetCenterPoint(CdblPoint &dblCenter);
	virtual ~CMatrixCalibratorView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif
    
	// Generated message map functions
protected:
	//{{AFX_MSG(CMatrixCalibratorView)
	afx_msg void OnPaint();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnDestroy();
	//}}AFX_MSG
	afx_msg void OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct);
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MATRIXCALIBRATORVIEW_H__F931E760_AFC6_41A8_A5C3_B8A1E74B8485__INCLUDED_)
